package com.ideal.gpt.app.docqajob.topicjob;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.ideal.gpt.app.docqajob.QaParseJob.QuestionType;
import com.ideal.gpt.app.docqajob.topicjob.ElementPick.Block;
import com.lzwork.utility.collection.CollUtility;
import com.lzwork.utility.pair.StringPair;
import com.lzwork.utility.str.StrUtility;

/**
 * @author LiZheng
 * @date 2023年11月9日 下午3:38:10
 */

public class PhoneJob
{
	public final static String prompt = "找出文中的关于电话号码的句子，并以json格式返回。";

	Map<String, String> phoneInfo = new HashMap<>();
	String[] keys =
	{};

	public float numPercent(String str)
	{
		if (!StrUtility.stringValid(str))
			return 0;

		float total = str.length();
		float n = 0;

		for (char c : str.toCharArray())
		{
			if (c >= '0' && c <= '9')
				n++;
		}

		return n / total;
	}

	public PhoneJob(List<Block> data)
	{
		for (Block b : data)
		{
			String currentNum = null;
			List<String> other = new ArrayList<>();

			for (StringPair sp : b)
			{
				String str = sp.second();
				float p = numPercent(str);
				if (p >= 0.7f)
					currentNum = str;
				else
					other.add(str);
			}

			if (currentNum != null)
				phoneInfo.put(currentNum, StrUtility.combineString(other, " "));
		}

		keys = phoneInfo.keySet().toArray(keys);
	}

	public JSONObject makeQuestion(QuestionType type)
	{
		if (phoneInfo.isEmpty())
			return null;

		if (type == QuestionType.Fill)
		{
			CollUtility.getRandomIndex(keys.length, 1);
		}
		
		return null;
	}
}
